package net.cl;

import java.util.regex.Matcher;


/**
 * 验证类
 * 
 * @author caol64@gmail.com
 */
public class Validator {

	public static boolean isNull(Object obj) {
		return obj == null;
	}

	public static boolean isNotNull(Object obj) {
		return obj != null;
	}

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0)
			return true;
		for (int i = 0; i < strLen; i++) {
			if (!Character.isWhitespace(str.charAt(i)))
				return false;
		}
		return true;
	}

	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}

	public static boolean isNumber(String str) {
		if (str == null)
			return Boolean.FALSE;
		return str.matches("[\\-]?[\\d]+[\\d]*(\\.[\\d]+)?");
	}

	public static boolean isBoolean(String str) {
		return "true".equalsIgnoreCase(str) || "false".equalsIgnoreCase(str);
	}

	public static boolean isNotNumber(String str) {
		return !isNumber(str);
	}

	public static boolean isNAN(String str) {
		return isNotNumber(str);
	}

	public static boolean isPositiveInteger(String str) {
		if (str == null)
			return Boolean.FALSE;
		return str.matches("^[0-9]*[1-9][0-9]*$");
	}

	public final static boolean isEmail(String email) {
		if (email == null)
			return Boolean.FALSE;
		return email.matches("(([\\w][\\.\\-]?)+[\\w])@([\\w\\-]+\\.)+[\\w]+");
	}

	public static boolean isMatch(String pattern, String str) {
		if (pattern == null || str == null)
			throw new NullPointerException();
		Matcher matcher = Provider.getPattern(pattern).matcher(str);
		return matcher.matches();
	}

	public static void main(String[] args) {
		System.out.println(isNumber("0"));
	}

}
